package com.li.controller;

import com.li.common.config.Result;
import com.li.mapper.LeaveMapper;
import com.li.pojo.Leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/leave")
public class LeaveController {
  @Autowired
  LeaveMapper LeaveMapper;
  
  //获取公告列表
  @GetMapping
  public Result<?> getLeavedents(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer rows) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("offset", ((page - 1) * rows));
    queryMap.put("pageSize", rows);
    ret.put("rows", LeaveMapper.findLeaveList(queryMap));
    ret.put("total", LeaveMapper.getLeaveTotal(queryMap));
    return Result.success(ret);
  }
  
  
  /**
   * 新增
   *
   * @param
   * @return
   */
  @PostMapping
  public Result<?> save(@RequestBody Leave Leave) {
    System.out.println(Leave);
  
    try {
      //自动更新创作时间
      Leave.setLtime(new Date());
      LeaveMapper.addleave(Leave);
      return Result.success();
    } catch (DataAccessException e) {
      return Result.error("-1", "添加失败!已有当前学号");
    }
    
    
  }
  
  
  @DeleteMapping("/{id}")
  public Result<?> deleteUser(@PathVariable String id) {
    Integer i = LeaveMapper.deleteById(id);
    if (i == 0) {
      return Result.error("-1", "删除失败");
    }
    return Result.success();
  }
  
}
